<?php

namespace app\controller;

use app\logic\OrderLogic;
use mikkle\tp_master\Cache;
use think\facade\Config;
use think\facade\Db;
use think\facade\Request;
use think\helper\Str;
use mikkle\tp_wxpay\Wxpay;
use mikkle\tp_alipay\Alipay;

class CommonPayController
{

    protected static $url = 'iwhycat.com';

    public function getPayCord($order, $body, $money)
    {
        $resultData = [
            'body' => $body, /**商品描述*/
            'out_trade_no' => $order, /**商户订单号*/
            'product_id' => $order,
            'total_fee' => $money, /**标价金额(单位分)*/
            'notify_url' => (string)url('/wl/user/payCallBack')->domain(self::$url), /**通知地址(WchatConfig::$notificationURL)*/
            'trade_type' => "NATIVE",/**交易类型  NATIVE  MWEB  */
        ];
        Cache::set('Lin-url', $resultData);
        $result = Wxpay::instance()->unifiedOrder()->setParam($resultData)->getPayUrl();
        return $result;
    }

    public function payCallBack()
    {
        Cache::set('Lin-is-run', '执行回调'.toDate(time()));
        try {
            $options = Config::get("wxpay." . Config::get("wxpay.default_options_name"));
            $callback = Wxpay::instance($options)->Notify();
            $result = $callback->getPostData();
            $result["sign_status"] = $callback->checkSign() ? 1 : 0;
            Cache::set('Lin-result', $result);
            $orderLogic = new \app\logic\OrderLogic();
            $retOrder = $orderLogic->successOrderCallBack($result);
            return response($callback->returnResult($retOrder), 200, [], 'xml');
        } catch (Exception $e) {
            Cache::set('Lin-error',$e->getMessage());
            Log::error($e->getMessage());
            return ShowCode::jsonCodeWithoutData(1008, $e->getMessage());
        }
    }

    /**
     * ali支付回调
     * @param Request $request
     *
     * @return \think\Response
     */
    public function aliCallBack(Request $request)
    {
        $param = $request::param();
        Cache::set('Lin-param', $param);
        try {
            $options = Config::get("alipay.".Config::get("alipay.default_options_name"));
            $callback = Alipay::instance($options)->Notify();
            $param["sign_status"] = $callback->verifyRsaSign($param) ? 1 : 0;
            Cache::set('Lin-result', $param);
            $orderLogic = new \app\logic\OrderLogic();
            return response($orderLogic->successAliOrderCallBack($param),200,[],'html');
        } catch (Exception $e) {
            Log::error($e->getMessage());
            return ShowCode::jsonCodeWithoutData(1008, $e->getMessage());
        }
    }

    public function getAliPayUrl($order, $body, $money){
        $options = Config::get("alipay.".Config::get("alipay.default_options_name"));
//        $d = file_get_contents('./app_private_key.pem');
        $url = Alipay::instance($options)->PagePay()
            ->setParam([
                "return_url"=>"http://iwhycat.com/#/google/user-search1",
                "notify_url"=>(string)url('/wl/user/aliCallBack')->domain(self::$url)
            ])
            ->setBizContentParam([
                "subject"=>$body,
                "out_trade_no"=>$order,
                "total_amount"=>$money * 0.01,
            ])
            ->getQuickPayUrl();
        return $url;
    }

    public function getCacheDump()
    {
        $param = Cache::get('Lin-param');
        $url = Cache::get('Lin-url');
        $result = Cache::get('Lin-result');
        dump($param);
        dump($url);
        dump($result);
        dump(Cache::get('Lin-is-run'));
        dump(Cache::get('Lin-error'));
    }

}
